<?php require VIEWPATH . ADMIN_DIR . DIRECTORY_SEPARATOR . 'header.php' ?>
<style>
	#import {
		padding-left: 0;
		padding-right: 0;
	}
	#import > div {
		height: 40px;
	}

</style>
<div class="page-header">
	<h1 style="font-size: 20px;">
		<a class="a-back" href="<?= admin_url('contents/result_major_data') ?>">
            <i class="fa fa-reply"></i>
        </a>
		<?= $title ?>
		<!-- 教师权限不可编辑 -->
		<?php if($rmxUser['user_type'] != USER_TYPE_TEACHER): ?>
			<!-- <small>
				<a href="javascript:" class="blue ml10" id="import2">
					<i class="ace-icon fa fa-upload"></i> 导入年级成绩数据
				</a>
			</small> -->
			<!-- <small>
				<a href="https://resource.rongmixiao.com/2020/02/17/292b2de2e3c044f78a98139bcf570a0c.xls" target="_blank" class="blue ml10">
					<i class="ace-icon fa fa-download"></i> 导出成绩模板
				</a>
			</small> -->
			<a href="<?= admin_url('contents/edit_result_grade_page') . '?major_id=' . $majorId ?>" class="btn btn-info a-custom" style="height: 42px;">
				<i class="ace-icon fa fa-plus"></i> 添加年级项
			</a>
			<a href="#modal-exam" class="btn btn-info a-custom" id="import" style="height: 42px;">
				&nbsp;&nbsp;&nbsp;&nbsp;<i class="ace-icon fa fa-upload"></i> 导入年级成绩数据&nbsp;&nbsp;&nbsp;&nbsp;
			</a>
		<?php endif ?>	
	</h1>
</div>
<div class="col-xs-12">
	<!-- <a href="<?= admin_url('contents/edit_result_grade_page') . '?major_id=' . $majorId ?>" class="btn btn-primary">
		<i class="ace-icon fa fa-plus"></i> 添加年级项
	</a> -->
	<table id="dynamic-table" class="table table-striped table-bordered table-hover" style="margin-top:10px">
		<thead>
			<tr>
				<th>年级名称</th>
				<th>是否显示年级排名</th>
				<th>是否显示班级排名</th>
				<th>考试科目</th>
				<th>添加时间</th>
				<th>学生成绩列表</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>

<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script type="text/javascript">
	var table = "";
	$(function(){
		// 考试列表
		table = $('#dynamic-table').dt({
			"ajaxUrl": '<?= admin_url('contents/get_result_grade_list/' . $majorId)?>',
			"columns": [
				{"data": function(ret){
	            	return ret.grade_name;
				}},
				{"data": function(ret){
	            	return ret.show_grade_rank_chinese;
				}},
				{"data": function(ret){
	            	return ret.show_class_rank_chinese;
				}},
				{"data": function(ret){
	            	return ret.subject_names;
				}},
				{"data": function(ret){
	            	return ret.createtime;
				}},
				{"data": function(ret){
	            	return '<a href="<?= admin_url('contents/result_data/')?>' + ret.id + '" class="mr10"><i class="fa fa-gears"></i> 查看</a>';
				}},
				{"data": function(ret){
					var action = '';
					action += '<a href="<?= admin_url('contents/edit_result_grade_page?major_id=' . $majorId . '&result_grade_id=')?>' + ret.id + '" class="mr10 text-success"><i class="fa fa-edit"></i> 编辑</a>&nbsp;&nbsp;';
					action += '<a data-id="' + ret.id + '" data-name="' + ret.grade_name + '" class="delete-result-grade text-danger mr20" href="javascript:;"><i class="fa fa-times"></i> 删除</a>';
					action += '<a href="<?= admin_url('contents/export_grade_result/') ?>' + ret.id + '" target="_blank" class="export-result mr10 text-primary"><i class="fa fa-download"></i> 导出成绩模板</a>&nbsp;&nbsp;';
					action += '<a data-id="' + ret.id + '" data-name="' + ret.grade_name + '" href="javascript:;" class="notify-result text-success"><i class="fa fa-bullhorn"></i> 发送成绩通知</a>&nbsp;&nbsp;';
					return action;
				}},
			]
		});
		// 删除某场考试
		$("body").on("click", ".delete-result-grade", function(){
			var id = $(this).data("id");
			var name = $(this).data("name");
			layer.confirm('确定删除年级项：' + name + '吗？', function(){
				// 提交数据
				var postUrl = "<?=admin_url('contents/delete_result_grade_action')?>";
				var postData = {"result_grade_id": id};
				ajax_post(postUrl, postData, function(res){
					table.draw(false);
				})
			});
		})
		// 导入年级成绩
		var upFromurl = '<?=UP_FORM_URL?>';
		var signature = '<?=$signature?>';
		var policy = '<?=$policy?>';
		$("#import").uploader({
			"server": upFromurl,
			"authorization": signature,
			"policy": policy,
			// 只支持上传 xls
			"accept": "xls",
			"callback": function(response) {
				// 上传后的文件名
				var fileLink = upyunUrl + response.url;
				var data = {
					fileLink: fileLink,
					major_id: <?= $majorId ?>
				}
				ajax_post("<?= admin_url('import/import_grade_result') ?>", data, function(res) {
					// 刷新页面
					location.reload();
				});
			}
		});
		// 发送成绩通知
		$("body").on("click", ".notify-result", function(){
			var id = $(this).data("id");
			var name = $(this).data("name");
			layer.confirm('确定发送通知到：' + name + '吗？', function(){
				// 提交数据
				var postUrl = "<?=admin_url('contents/notify_grade_result/')?>" + id;
				ajax_post(postUrl, {}, function(res){
					window.location.reload();
				})
			});
		})
	})
</script>